package cn.ad.server.domain.ad;

import cn.ad.server.domain.enumration.ResourceType;
import org.joda.time.DateTime;

import java.io.Serializable;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;

public class StrategyGroup implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = -770741153445643149L;

	static final String VERSION = "0";
	static final String CACHE_NAME = "strategygroup";

	private int id;
	private String name;
	private int campaignId;
	private ResourceType resourceType;// 资源类型
	private BigDecimal dayBudget;
	private int speedMode;
	private int status;
	private Date createTime;
	private Date updateTime;
	/**
	 * 暂时不使用。
	 */
	private DayTargert dayTarget;

	private List<StrategyGroupPlatformMap> strategyGroupPlatformMaps;

	/**
	 * 存放的是 yyyymmdd : 当天可用的小时数
	 */
	private Map<String, Integer> usableHour = new HashMap<>();

	/**
	 * 存放的是 yyyymmdd : 当天的定向。(1110110101011)
	 */
	private Map<String, Integer> dayTargetMap = new HashMap<>();

	private List<String> usableHourKey = new ArrayList<>();

	private List<Integer> usableHourValue = new ArrayList<>();

	@Deprecated
	public String dayBudgetKey() {
		String date = DateTime.now().toString("yyyy-MM-dd");
		return new StringBuilder(CACHE_NAME).append(":").append(this.getId()).append(":").append(date).append(":")
				.append("day.budget").toString();
	}

	public static String dayBudgetKey(long id) {
		String date = DateTime.now().toString("yyyy-MM-dd");
		return new StringBuilder(CACHE_NAME).append(":").append(id).append(":").append(date).append(":")
				.append("day.budget").toString();
	}

	@Deprecated
	public String timeBucketKey() {
		String date = DateTime.now().toString("yyyy-MM-dd");
		String base = "bucket";
		return new StringBuilder(CACHE_NAME).append(":").append(this.getId()).append(":").append(date).append(":")
				.append("time.bucket").append(":").append(base).toString();
	}

	public static String timeBucketKey(long id) {
		String date = DateTime.now().toString("yyyy-MM-dd");
		String base = "bucket";
		return new StringBuilder(CACHE_NAME).append(":").append(id).append(":").append(date).append(":")
				.append("time.bucket").append(":").append(base).toString();
	}

	@Deprecated
	public String latestConsumeKey() {
		String date = DateTime.now().toString("yyyy-MM-dd");
		String base = "bucket";
		return new StringBuilder(CACHE_NAME).append(":").append(this.getId()).append(":").append(date).append(":")
				.append("latest.consume").append(":").append(base).toString();
	}

	public static String latestConsumeKey(long id) {
		String date = DateTime.now().toString("yyyy-MM-dd");
		String base = "bucket";
		return new StringBuilder(CACHE_NAME).append(":").append(id).append(":").append(date).append(":")
				.append("latest.consume").append(":").append(base).toString();
	}

	@Deprecated
	public String addAmountCountKey(long lastTime) {
		String date = DateTime.now().toString("yyyy-MM-dd");
		return new StringBuilder(CACHE_NAME).append(":").append(this.getId()).append(":").append(date)
				.append(":latest.add:").append(lastTime).toString();
	}

	public static String addAmountCountKey(long id, long lastTime) {
		String date = DateTime.now().toString("yyyy-MM-dd");
		return new StringBuilder(CACHE_NAME).append(":").append(id).append(":").append(date).append(":latest.add:")
				.append(lastTime).toString();
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getCampaignId() {
		return campaignId;
	}

	public void setCampaignId(int campaignId) {
		this.campaignId = campaignId;
	}

	public ResourceType getResourceType() {
		return resourceType;
	}

	public void setResourceType(ResourceType resourceType) {
		this.resourceType = resourceType;
	}

	public BigDecimal getDayBudget() {
		return dayBudget;
	}

	public void setDayBudget(BigDecimal dayBudget) {
		this.dayBudget = dayBudget;
	}

	public int getSpeedMode() {
		return speedMode;
	}

	public void setSpeedMode(int speedMode) {
		this.speedMode = speedMode;
	}

	public Date getCreateTime() {
		return createTime;
	}

	public void setCreateTime(Date createTime) {
		this.createTime = createTime;
	}

	public Date getUpdateTime() {
		return updateTime;
	}

	public void setUpdateTime(Date updateTime) {
		this.updateTime = updateTime;
	}

	public int getStatus() {
		return status;
	}

	public void setStatus(int status) {
		this.status = status;
	}

	public List<StrategyGroupPlatformMap> getStrategyGroupPlatformMaps() {
		return strategyGroupPlatformMaps;
	}

	public void setStrategyGroupPlatformMaps(List<StrategyGroupPlatformMap> strategyGroupPlatformMaps) {
		this.strategyGroupPlatformMaps = strategyGroupPlatformMaps;
	}

	public Map<String, Integer> getUsableHour() {
		return usableHour;
	}

	public void setUsableHour(Map<String, Integer> usableHour) {
		this.usableHour = usableHour;
	}

	private void usablehourPut(String key, Integer total) {
		if (usableHour == null) {
			usableHour = new HashMap<>();
		}
		usableHour.put(key, total);
	}

	public void putUsablehour(Date key, Integer total) {
		String strKey = format.format(key);
		usablehourPut(strKey, total);
	}

	/**
	 * 根据 date 得到当天可用的小时数
	 * 
	 * @param key
	 * @return
	 */
	public Integer getUsablehour(Date key) {

		String strKey = format.format(key);

		if (usableHour == null) {
			return null;
		}
		return usableHour.get(strKey);
	}

	private final static SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");

	public List<String> getUsableHourKey() {
		return usableHourKey;
	}

	public void setUsableHourKey(List<String> usableHourKey) {
		this.usableHourKey = usableHourKey;
	}

	public List<Integer> getUsableHourValue() {
		return usableHourValue;
	}

	public void setUsableHourValue(List<Integer> usableHourValue) {
		this.usableHourValue = usableHourValue;
	}

	public void addUsableHour(Date key, Integer total) {
		String strKey = format.format(key);
		usableHourKey.add(strKey);
		usableHourValue.add(total);
	}

	public Integer getUsableHour(Date key) {
		String strKey = format.format(key);
		for (int i = 0; i < usableHourKey.size(); i++) {
			if (strKey.equals(usableHourKey.get(i))) {
				return usableHourValue.get(i);
			}
		}
		return null;
	}

	public DayTargert getDayTarget() {
		return dayTarget;
	}

	public void setDayTarget(DayTargert dayTarget) {
		this.dayTarget = dayTarget;
	}

	public Map<String, Integer> getDayTargetMap() {
		return dayTargetMap;
	}

	public void setDayTargetMap(Map<String, Integer> dayTargetMap) {
		this.dayTargetMap = dayTargetMap;
	}

	public void putDayTargetMap(Date key, Integer total) {
		String strKey = format.format(key);
		dayTargetMapPut(strKey, total);
	}

	private void dayTargetMapPut(String key, Integer total) {
		if (dayTargetMap == null) {
			dayTargetMap = new HashMap<>();
		}
		dayTargetMap.put(key, total);
	}

	/**
	 * 根据 date 得到当天的定向。(1110110101011)
	 * 
	 * @param key
	 * @return
	 */
	public Integer getDayTargetMap(Date key) {
		String strKey = format.format(key);

		if (dayTargetMap == null) {
			return null;
		}
		return dayTargetMap.get(strKey);
	}

}
